﻿using Grpc.Core;
using gRPCService.Protos;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace gRPCService.Services
{
    public class LuCatService: LuCat.LuCatBase
    {

        private static readonly List<string> Cats = new List<string>
        {
            "肥猫",
            "死猫",
            "大橘",
            "英短"
        };

        private readonly ILogger<LuCatService> _logger;
        public LuCatService(ILogger<LuCatService> logger)
        {
            _logger = logger;
        }


        public override async Task BatchThaCat(IAsyncStreamReader<BathTheCatReq> requestStream, IServerStreamWriter<BathTheCatResp> responseStream, ServerCallContext context)
        {
            //return base.BatchThaCat(requestStream, responseStream, context);

            var batchQueue = new Queue<int>();
            while (await requestStream.MoveNext())
            {
                batchQueue.Enqueue(requestStream.Current.Id);
                _logger.LogInformation($" Cat {requestStream.Current.Id} Enqueue");
            }

            while (batchQueue.TryDequeue(out var catid))
            {
                await responseStream.WriteAsync(new BathTheCatResp() { Message = $"成功撸了一只{Cats[catid]}" });
                await Task.Delay(5000);
            }
        }
    }
}
